package com.example.server.intercept;

import com.example.server.trasaction.HaeTransactionManager;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: yuanjianpeng
 * @Date: 2019/5/18 23:21
 * @Description: 请求拦截，以获取事务组id和transactionCount
 */
public class RequestIntercept implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        try {
            HaeTransactionManager.setCurrentGroupId(request.getHeader("groupId"));
            Integer transactionCount = Integer.valueOf(request.getHeader("transactionCount"));
            HaeTransactionManager.setTransactionCount(transactionCount == null ? 0 : transactionCount);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
